<?php
declare (strict_types = 1);

namespace app\api\controller\auth;

use app\api\controller\Api;
use app\common\system\logic\AuthNodeLogic;
use app\common\system\logic\AuthRoleLogic;
use app\common\system\logic\UserLogic;
use app\common\system\logic\UserRoleLogic;

class Menu extends Api
{
    public function index()
    {
        $user = $this->request->user;
        if ($user['id'] == 1) {
            $nodeLogic = new AuthNodeLogic();
            $list = $nodeLogic->getMenuItems();
        } else {
            $userLogic = new UserLogic();
            $user = $userLogic->getUser($user['id'], ['id','role_id']);
            if (empty($user['role_id'])) {
                $list = [];
            } else {
                $authRoleLogic = new AuthRoleLogic();
                $role = $authRoleLogic->getRole($user['role_id']);
                $role->visible(['pc']);
                

                $nodeLogic = new AuthNodeLogic();
                $list = $nodeLogic->getMenuByIds($role['pc']);
            }
        }
        $routes = build_menu([], $list, 0);
        $result['routes'] = $routes;
        return success($result, '获取菜单成功');
    }
}
